<!DOCTYPE html>
<!--
 * Copyright (c) 2011 Jack Senechal. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
-->
<!--
 * Copyright (c) 2011 Vincent Qiu. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
-->
<html>
<head>
    <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="js/common.js"></script>
    <script type="text/javascript" src="options.js"></script>
    <script>
        var email;
        // Check that options are set. If not, set them using the defaults from options.js
        for (key in defaults) {
            //            if (!localStorage[key]) {
            localStorage[key] = defaults[key];
            //            }
        }

        var manifest;

        getManifest(function (manifestParam) {
            manifest = manifestParam;
            if (!localStorage["installDate"]) {
                localStorage["installDate"] = new Date();
                localStorage["installVersion"] = manifest.version;
            }
            if (localStorage["version"]) {
                if (localStorage["version"] != manifest.version) {
                    /* Jason: uncomment if important update */
                    //chrome.tabs.create({url:"options.html?update=true"});
                }
            } else {
                //chrome.tabs.create({url: "options.html?install=true"});
            }
            localStorage["version"] = manifest.version;
        });

        // Called when a message is passed.
        function onRequest(request, sender, sendResponse) {
            switch (request.name) {
                case "getLocalStorage":
                    sendResponse(
					{
					    ls: localStorage,
					}
				);
                    break;
                case "setLocalStorage":
                    localStorage[request.key] = request.value;
                    sendResponse(
					{
					    blah: "blah",
					}
				);
                    break;
                case "sendGA":
                    sendGA(request.value);
                    break;
                case "saveEmail":
                    email = request.email;
                    break;
                case "insertCSS":
                    chrome.tabs.insertCSS(null, { code: request.css, allFrames: true }, function () {
                        alert('inserted done')
                    });
                    break;
            }
            if (request.action == 'options') {
                // Send the localStorage variable to the content script so that it
                // can use the options set in the options page
                //    console.log("options requset");
                sendResponse({ options: localStorage });
            } else if (request.action == 'showPageAction') {
                // Show the page action for the tab that the sender (content script)
                // was on.
                chrome.pageAction.show(sender.tab.id);
                //    console.log("showPageAction requset");
            }
            else
                // Return nothing to let the connection be cleaned up.
                //console.log("clean up requset");
                sendResponse({});
        };

        // Listen for the content script to send a message to the background page.
        chrome.extension.onRequest.addListener(onRequest);
    </script>
</head>
</html>
